package models;

import play.db.jpa.Model;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Column;
import javax.persistence.Transient;
import play.libs.F;
import java.util.HashMap;
import java.util.Map;
import java.util.Collections;

/**
 * @author Andriy
 * */
@Entity
public class Provs extends Model implements java.io.Serializable {

        @Column(unique = true, nullable = false)
        public String login;
        @Column(nullable = false)
        public String password;
        @Enumerated(EnumType.STRING)
        public Status status;
        public String name;
        public String lastname;
        public String zip;
        public String city;
        public String stateProvince;
        public String country;
        public String email;
        public String typeOfCar;
        public byte seatsNumber;
        public float fareFixed;
        public float farePerMinute;
        /**
         *	Collection which contain id of providers and its event stream
         **/
        @Transient
        private static Map<Long, F.ArchivedEventStream<models.StatusManager.Event>> events = Collections.synchronizedMap(new HashMap<Long, F.ArchivedEventStream<models.StatusManager.Event>>(50));

        public Provs() {
        }

        public Provs(String login, String name, String lastname, String zip, String city, String stateProvince, String country, String email, String typeOfCar, byte seatsNumber, float fareFixed, float farePerMinute, String password) {
                this.login = login;
                this.name = name;
                this.lastname = lastname;
                this.zip = zip;
                this.city = city;
                this.stateProvince = stateProvince;
                this.country = country;
                this.email = email;
                this.typeOfCar = typeOfCar;
                this.seatsNumber = seatsNumber;
                this.fareFixed = fareFixed;
                this.farePerMinute = farePerMinute;
                this.password = Encrypt.encrypt(password);
                this.status = Status.p_declared; //must be override;
        }

        public Provs(String login, String password) {
                this.login = login;
                this.password = Encrypt.encrypt(password);
        }

        /**
         * @return event stream of provider
         * */
        public static F.ArchivedEventStream<models.StatusManager.Event> getEventStream(long provId) {
                if (Provs.events.get(provId) == null) {
                        F.ArchivedEventStream<models.StatusManager.Event> eventStream = new F.ArchivedEventStream<models.StatusManager.Event>(50);
                        Provs.events.put(provId, eventStream);
                }
                return Provs.events.get(provId);

        }

        public static void setStatus(long provId, Status status) {
                Provs provs = Provs.find("byId", provId).first();
                provs.status = status;
                provs.save();
        }

        public static Status getStatus(long provId)
        {
                Provs provs = Provs.find("byId", provId).first();
                return provs.status;
        }

        /**
         * Remove event stream of provider.
         * */
        public static void delEventStream(long provId) {
                Provs.events.remove(provId);
        }
}
